package com.mentalhealth.diary.controller;

import com.mentalhealth.diary.dto.BindPhoneDTO;
import com.mentalhealth.diary.dto.RefreshTokenDTO;
import com.mentalhealth.diary.dto.SendCodeDTO;
import com.mentalhealth.diary.service.AuthService;
import com.mentalhealth.diary.vo.LoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 用户控制器 - V1版本
 */
@RestController
@RequestMapping("/v1/user")
public class UserV1Controller {

    @Autowired
    private AuthService authService;

    /**
     * 发送绑定手机号验证码
     */
    @PostMapping("/bind-phone/sms")
    public Result<String> sendBindCode(@Validated @RequestBody SendCodeDTO sendCodeDTO) {
        authService.sendBindCode(sendCodeDTO);
        return Result.success("验证码已发送，请注意查收");
    }

    /**
     * 绑定手机号
     * 注意：实际项目中应该从JWT中获取用户ID，这里简化处理
     */
    @PostMapping("/bind-phone")
    public Result<LoginVO> bindPhone(@RequestAttribute("userId") Long userId, 
                                    @Validated @RequestBody BindPhoneDTO bindPhoneDTO) {
        LoginVO loginVO = authService.bindPhone(userId, bindPhoneDTO);
        return Result.success("手机号绑定成功～ 后续可使用密码登录", loginVO);
    }

    /**
     * 令牌刷新
     */
    @PostMapping("/auth/refresh-token")
    public Result<LoginVO> refreshToken(@Validated @RequestBody RefreshTokenDTO refreshTokenDTO) {
        LoginVO loginVO = authService.refreshToken(refreshTokenDTO.getOldToken());
        return Result.success("令牌刷新成功", loginVO);
    }

    /**
     * 统一响应结果类
     */
    public static class Result<T> {
        private Integer code;
        private String message;
        private T data;

        public Result() {
        }

        public Result(Integer code, String message, T data) {
            this.code = code;
            this.message = message;
            this.data = data;
        }

        public static <T> Result<T> success(String message, T data) {
            return new Result<>(200, message, data);
        }

        public static <T> Result<T> success(T data) {
            return new Result<>(200, "success", data);
        }

        public static <T> Result<T> success(String message) {
            return new Result<>(200, message, null);
        }

        public static <T> Result<T> error(String message) {
            return new Result<>(500, message, null);
        }

        public static <T> Result<T> error(Integer code, String message) {
            return new Result<>(code, message, null);
        }

        // getter and setter
        public Integer getCode() {
            return code;
        }

        public void setCode(Integer code) {
            this.code = code;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public T getData() {
            return data;
        }

        public void setData(T data) {
            this.data = data;
        }
    }
}